import config from "@/config"; // 配置文件
import store from "../store/index.js"; // 缓存封装
// #ifdef H5
import y_uni from '@/utils/webview';
// const baseUrl = 'http://192.168.100.64'
// http://10.129.60.31
const baseUrl = process.env.NODE_ENV === 'development' ? '/api' : 'http://192.168.100.64'

// const baseUrl = '/api'
// #endif    
// #ifndef H5
// const baseUrl = 'http://10.129.60.31'
//const baseUrl = 'http://192.168.100.64'
// const baseUrl = process.env.NODE_ENV === 'development' ? '/api' : 'http://10.129.60.31'
// const baseUrl = 'http://192.168.20.211:8071'
const baseUrl = 'http://192.168.100.64'

// #endif



export default {
	console(options) {
		if (config.debug) {
			// console.log("<<===============================================>>");
			// console.log("request start");
			// console.log("header" + JSON.stringify(options.header));
			// console.log("method: " + options.method + " URL: " + options.url);
			// console.log(options.data);
			// console.log("request end");
			// console.log("<<===============================================>>");
		}
	},
	domain() {
		return config.uni_app_web_api_url.replace("api", "");
	},
	send(options = {}) {
		// console.log(store.state)
		// this.storeLoading(true)
		// console.log('options22',options)

		console.log(process.env.NODE_ENV === 'development');

		if (options.data.loading == false) {


		} else {
			store.state.isLoading = true
		}

		// 拼接路劲，下面的配置文件会提到
		// options.url = config.uni_app_web_api_url + '' + options.url;
		// 请求方式
		options.method = options.method || "GET";

		// 这里看项目的情况来定，如果是没有token，那就删除这里，上面的storage也不需要引入
		const hasLogin = store.state.hasLogin;
		if (hasLogin) {
			options.header = {
				"satoken": uni.getStorageSync('access_token') || store.state.access_token,
				'content-type': options.r_type || 'application/json',
			};
		}

		// this.console(options); // 打印请求数据，调试用，上线可以注释
		// console.log('process', process.env.NODE_ENV);
		// console.log('options', options);


		options.url = baseUrl + options.url;
		console.log(' baseUrl + options.url', baseUrl + '---' + options.url);
		// 同步获取系统信息
		// const systemInfo = uni.getSystemInfoSync();
		// console.log(systemInfo);
		// 	if (systemInfo.platform === "windows") {
		// 		options.url = '/api' + options.url;
		// 		// options.url = 'http://192.168.113.200' + options.url;
		// 		// options.url = 'http://172.18.88.20' + options.url;

		// 		// http://192.168.20.29:8070
		// 	} else if (systemInfo.platform === "android") {
		// 		// options.url = 'http://192.168.20.29:8070' + options.url
		// 		options.url = 'http://192.168.20.211:8071' + options.url
		// 		// options.url = '/api' + options.url;
		// 		// options.url = 'http://192.168.100.64' + options.url
		// 	} 


		// console.log(`options ${options.url.split('ipad')[1]} 请求参数信息`, options);

		// 发起Promise请求
		return new Promise((resolve, reject) => {

			uni.request({
				timeout: 1000 * 10,
				...options
			}).then(data => {
				console.log('request--options 92', JSON.stringify(options))
				console.log('request--options 92', uni.getStorageSync('access_token'))

				setTimeout(() => {
					store.state.isLoading = false
				}, 80)
				if (data.data) {
					resolve(data.data)
					if (data.data.code != 200) {
						uni.showToast({
							title: data.data.msg || '请求失败！',
							icon: 'none'
						})


						let webview = uni.getStorageSync('webview') || ''
						console.log('webview', webview);

						if (data.data.code == 400 || data.data.code == 401 || data.data.code == 4020) {

							if (webview) {
								setTimeout(() => {
									try {
										y_uni.postMessage({
											data: {
												action: 'logout'
											}
										});
										y_uni.getEnv(function(res) {
											console.log('当前环境：' + JSON.stringify(res));
										});
									} catch (err) {
										console.log('err', err);
									}
								}, 500)
							} else {
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/login/login'
									})
								}, 500)
							}







						}

					}
				} else {
					store.state.isLoading = false
					reject(data);

				}

			}).catch((error) => {
				// this.storeLoading(false)
				store.state.isLoading = false
				reject(error);
			});
		});
	},
	get(url = "", data = {}) {
		return this.send({
			url: url,
			data: data
		});
	},
	post(url = "", data = {}, r_type = 'application/json') {
		console.log("URL", url)
		return this.send({
			url: url,
			data: data,
			method: "POST",
			r_type
		});
	},
	put(url = "", data = {}) {
		return this.send({
			url: url,
			data: data,
			method: "PUT"
		});
	}
};